<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>decorator模式 &mdash; programming v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="top" title="programming v0.1 documentation" href="../index.html" />
    <link rel="up" title="设计模式学习笔记" href="index.html" />
    <link rel="next" title="factory method模式" href="factory.html" />
    <link rel="prev" title="observer模式" href="observer.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="factory.html" title="factory method模式"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="observer.html" title="observer模式"
             accesskey="P">上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="decorator">
<h1>decorator模式<a class="headerlink" href="#decorator" title="永久链接至标题">¶</a></h1>
<div class="section" id="id1">
<span id="index-4"></span><span id="id2"></span><h2>decorator模式定义<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p>Decorator: Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for
extending functionality.</p>
<p>Decorators: 向一个对象 <strong>动态</strong> 地绑定额外的 <strong>功能（责任）</strong>.
Decorators 向为了实现扩展功能而提供一种除继承外更加 <strong>灵活</strong> 的方法。</p>
</div>
<div class="section" id="id3">
<h2>问题描述<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>一家咖啡店随着规模扩大，除了提供常见的咖啡外，还向顾客提供诸如加 <em>mocha</em>, <em>whip</em>, <em>soy</em> 等
辅料，所以现在需要解决</p>
<ul class="simple">
<li>如果增加辅料的类型如何处理</li>
<li>如果增加咖啡的类型如何处理等</li>
</ul>
<p>最开始的设计如下(完全使用继承）：</p>
<img alt="../_images/all_inheritance.png" src="../_images/all_inheritance.png" />
<p>更好的办法是使用 <strong>Decorators</strong> 模式，那么会很好地满足需求。</p>
<p>使用 <strong>Decorators</strong> 后的类图如下：</p>
<img alt="../_images/decorator.png" src="../_images/decorator.png" />
<p>现实中的例子包括，java中的IO，
如下图所示：</p>
<img alt="../_images/javaio.png" src="../_images/javaio.png" />
</div>
<div class="section" id="id4">
<h2>具体代码<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>下载请点击 <a href="../_downloads/decorator.cpp"><strong class="xref">这里</strong></a></p>
<div class="highlight-c++"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129</pre></td><td class="code"><div class="highlight"><pre><span class="cp">#include &lt;iostream&gt;</span>
<span class="cp">#include &lt;string&gt;</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Beverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">string</span> <span class="n">description</span><span class="p">;</span>
        <span class="kt">double</span> <span class="n">costs</span><span class="p">;</span>
        <span class="kt">int</span> <span class="n">size</span><span class="p">;</span>   <span class="c">//1,2,3 =&gt; small, medium, big</span>
        <span class="k">virtual</span> <span class="n">string</span> <span class="n">getDescription</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">string</span> <span class="n">size</span><span class="p">;</span>
            <span class="n">size</span> <span class="o">=</span> <span class="p">(</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">size</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span><span class="o">?</span><span class="s">&quot;small&quot;</span><span class="o">:</span><span class="p">((</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">size</span><span class="o">==</span><span class="mi">2</span><span class="p">)</span><span class="o">?</span><span class="s">&quot;medium&quot;</span><span class="o">:</span><span class="s">&quot;big&quot;</span><span class="p">);</span>
            <span class="k">return</span> <span class="n">size</span> <span class="o">+</span> <span class="s">&quot;  &quot;</span> <span class="o">+</span> <span class="n">description</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">virtual</span> <span class="kt">double</span> <span class="n">cost</span><span class="p">(){</span><span class="k">return</span> <span class="n">costs</span><span class="o">*</span><span class="n">size</span><span class="p">;}</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">setSize</span><span class="p">(</span><span class="kt">int</span> <span class="n">size</span><span class="p">){</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">size</span><span class="o">=</span><span class="n">size</span><span class="p">;}</span>
        <span class="k">virtual</span> <span class="kt">int</span> <span class="n">getSize</span><span class="p">(){</span><span class="k">return</span> <span class="n">size</span><span class="p">;}</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">printout</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">getDescription</span><span class="p">()</span><span class="o">&lt;&lt;</span><span class="s">&quot;  costs:&quot;</span><span class="o">&lt;&lt;</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">cost</span><span class="p">()</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">CondimentDecorator</span><span class="o">:</span><span class="k">public</span> <span class="n">Beverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Beverage</span> <span class="o">*</span><span class="n">beverage</span><span class="p">;</span>
        <span class="n">CondimentDecorator</span><span class="p">(</span><span class="n">Beverage</span> <span class="o">*</span><span class="n">beverage</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">size</span> <span class="o">=</span> <span class="n">beverage</span><span class="o">-&gt;</span><span class="n">getSize</span><span class="p">();</span>
        <span class="p">}</span>
        <span class="k">virtual</span> <span class="kt">double</span> <span class="n">cost</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">beverage</span><span class="o">-&gt;</span><span class="n">cost</span><span class="p">()</span> <span class="o">+</span> <span class="n">costs</span><span class="o">*</span><span class="n">beverage</span><span class="o">-&gt;</span><span class="n">getSize</span><span class="p">();</span>
        <span class="p">}</span>
        <span class="k">virtual</span> <span class="n">string</span> <span class="n">getDescription</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">beverage</span><span class="o">-&gt;</span><span class="n">getDescription</span><span class="p">()</span> <span class="o">+</span> <span class="s">&quot;, &quot;</span> <span class="o">+</span> <span class="n">description</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="c">/*</span>
<span class="c">        virtual ~CondimentDecorator()</span>
<span class="c">        {</span>
<span class="c">            cout&lt;&lt;beverage-&gt;getDescription()&lt;&lt;&quot;  was called.&quot;&lt;&lt;endl;</span>
<span class="c">            delete beverage;</span>
<span class="c">        }</span>
<span class="c">        */</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">HouseBlend</span><span class="o">:</span><span class="k">public</span> <span class="n">Beverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">HouseBlend</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;HouseBlend&quot;</span> <span class="p">;</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">.89</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Espresso</span><span class="o">:</span><span class="k">public</span> <span class="n">Beverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Espresso</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Espresso&quot;</span><span class="p">;</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">1.99</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">DarkRoast</span><span class="o">:</span><span class="k">public</span> <span class="n">Beverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">DarkRoast</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;DarkRoast&quot;</span><span class="p">;</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">.99</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Decaf</span><span class="o">:</span><span class="k">public</span> <span class="n">Beverage</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Decaf</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">1.05</span><span class="p">;</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Decaf&quot;</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Mocha</span><span class="o">:</span><span class="k">public</span> <span class="n">CondimentDecorator</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Mocha</span><span class="p">(</span><span class="n">Beverage</span> <span class="o">*</span><span class="n">beverage</span><span class="p">)</span><span class="o">:</span><span class="n">CondimentDecorator</span><span class="p">(</span><span class="n">beverage</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">beverage</span> <span class="o">=</span> <span class="n">beverage</span><span class="p">;</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">;</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Mocha&quot;</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Soy</span><span class="o">:</span><span class="k">public</span> <span class="n">CondimentDecorator</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Soy</span><span class="p">(</span><span class="n">Beverage</span> <span class="o">*</span><span class="n">beverage</span><span class="p">)</span><span class="o">:</span><span class="n">CondimentDecorator</span><span class="p">(</span><span class="n">beverage</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">beverage</span> <span class="o">=</span> <span class="n">beverage</span><span class="p">;</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">0.15</span><span class="p">;</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Soy&quot;</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Whip</span><span class="o">:</span><span class="k">public</span> <span class="n">CondimentDecorator</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Whip</span><span class="p">(</span><span class="n">Beverage</span> <span class="o">*</span><span class="n">beverage</span><span class="p">)</span><span class="o">:</span><span class="n">CondimentDecorator</span><span class="p">(</span><span class="n">beverage</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">beverage</span> <span class="o">=</span> <span class="n">beverage</span><span class="p">;</span>
            <span class="n">costs</span> <span class="o">=</span> <span class="mf">0.10</span><span class="p">;</span>
            <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Whip&quot;</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>


<span class="kt">int</span> <span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">DarkRoast</span> <span class="n">d</span><span class="p">;</span>
    <span class="n">d</span><span class="p">.</span><span class="n">setSize</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
    <span class="n">Whip</span> <span class="n">w</span><span class="p">(</span><span class="k">new</span> <span class="n">Soy</span><span class="p">(</span><span class="k">new</span> <span class="n">Whip</span><span class="p">(</span><span class="k">new</span> <span class="n">Mocha</span><span class="p">(</span><span class="o">&amp;</span><span class="n">d</span><span class="p">))));</span>  <span class="c">// add the decorator as many as you need</span>
    <span class="n">w</span><span class="p">.</span><span class="n">printout</span><span class="p">();</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
        
</pre></div>
</td></tr></table></div>
<p>输出结果为:</p>
<div class="highlight-python"><pre>medium  DarkRoast, Mocha, Whip, Soy, Whip  costs:3.08</pre>
</div>
</div>
<div class="section" id="id5">
<h2>面对新的需要<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p>当需要增加新的辅料时，只需要：</p>
<ul class="simple">
<li>继承 <strong>CondimentDecorator</strong> 类，</li>
<li>实现其中的相关操作</li>
</ul>
</div>
<div class="section" id="id6">
<h2>更多注意<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li><strong>Decorators</strong> 会导致出现很多一些小类（装饰类），导致类特别多。</li>
<li>如果要在具体的 <em>Component</em> 类中有相关的行为，则可能不适用（如在DarkRoast中有打折相关的行为）</li>
<li><strong>DarkRoast</strong> 模式通常会和 <strong>Factory</strong> 或者 <strong>Builder</strong> 模式一起使用</li>
</ul>
<p>新的设计原则：</p>
<p><strong>Classed should be open for extension but closed for modification.</strong></p>
</div>
<div class="section" id="id7">
<h2>参考更多<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator_Pattern</a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference external" href="">decorator模式</a><ul>
<li><a class="reference external" href="#id1">decorator模式定义</a></li>
<li><a class="reference external" href="#id3">问题描述</a></li>
<li><a class="reference external" href="#id4">具体代码</a></li>
<li><a class="reference external" href="#id5">面对新的需要</a></li>
<li><a class="reference external" href="#id6">更多注意</a></li>
<li><a class="reference external" href="#id7">参考更多</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="observer.html"
                                  title="上一章">observer模式</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="factory.html"
                                  title="下一章">factory method模式</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/designpattern/decorator.txt"
                     rel="nofollow">显示源代码</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>快速搜索</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="搜索" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              输入相关的模块，术语，类或者函数名称进行搜索
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="factory.html" title="factory method模式"
             >下一页</a> |</li>
        <li class="right" >
          <a href="observer.html" title="observer模式"
             >上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" >设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 版权所有 2009, zhutao.iscas@gmail.com.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>